package com.yanqu.road.server.cmd.activity.rank;

import com.yanqu.road.entity.enums.eGamePlayerEventType;
import com.yanqu.road.logic.cross.CrossActivityTeamRankListModel;
import com.yanqu.road.pb.cross.activity.CrossActivityProto;
import com.yanqu.road.server.cmd.CrossCommand;
import com.yanqu.road.server.cmd.CrossUnionCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.logic.MessageHelper;
import com.yanqu.road.server.manager.activity.ActivityTeamRankMgr;
import com.yanqu.road.server.manager.douluodalu.CrossDouLuoDaLuContext;
import com.yanqu.road.utils.YanQuMessageUtils;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.CrossProtocol;
import com.yanqu.road.server.protocol.Protocol;

@Cmd(code = CrossProtocol.C2_CROSS_GET_ACTIVITY_TEAM_RANK, desc = "查看跨服活动队伍榜单")
public class GetCrossActivityTeamRankListCmd extends CrossUnionCommand {

    private long userId;
    private int activityId;
    private int bigRankType;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        CrossActivityProto.CrossActivityTeamRankListReqMsg reqMsg = CrossActivityProto.CrossActivityTeamRankListReqMsg.parseFrom(packet.getBytes());
        activityId = reqMsg.getActivityId();
        userId = packet.getPlayerId();
        bigRankType = reqMsg.getConditionType();
        return true;
    }

    @Override
    public void execute(YanQuMessage packet) throws Exception {
        CrossActivityProto.CrossActivityTeamRankListRespMsg.Builder respMsg = CrossActivityProto.CrossActivityTeamRankListRespMsg.newBuilder();
        respMsg.setActivityId(activityId);
        respMsg.setConditionType(bigRankType);
        respMsg.setMyRank(-1);
        int teamId = 0;
        long teamServerId = 0;
        if(bigRankType == eGamePlayerEventType.DouLuoDaLuPhaseTwoTeamScoreCrossRank.getValue()){
            CrossDouLuoDaLuContext context = CrossDouLuoDaLuContext.initContext(activityId, userId).loadTeamData();
            if(context.getRet() == 0){
                teamId = context.getTeamData().getTeamId();
                teamServerId = context.getTeamData().getCaptainServerId();
                respMsg.setMyValue(context.getActivity().getTeamScore(context.getTeamData()).toString());
            }
        }
        CrossActivityTeamRankListModel teamRankListModel = ActivityTeamRankMgr.getCrossActivityTeamRankListModel(activityId, bigRankType, groupId);
        if(teamRankListModel != null){
            respMsg.setRankList(teamRankListModel.getRankListMsg());
            if(teamId > 0){
                respMsg.setMyRank(teamRankListModel.getMyRank(teamId, teamServerId));
            }
        }else {
            respMsg.setRankList(CrossActivityProto.TeamRankListMsg.newBuilder());
        }

        YanQuMessage message = YanQuMessageUtils.buildMessage(Protocol.U_CROSS_ACTIVITY_TEAM_RANK_LIST, respMsg);
        MessageHelper.sendPacket(serverId, userId, message);
    }

    @Override
    public void sendData() {

    }
}
